//
//  RadioViewController.m
//  瞬间
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RadioViewController.h"
#import "AFNetworking.h"
#import "UIView+WebCacheOperation.h"
#import "hotlist.h"
#import "Scroimage.h"
#import "UIImageView+WebCache.h"
@interface RadioViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UIView *headerView;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIImageView *imageView;
@property (nonatomic, retain)NSMutableArray *imageArr;

@end

@implementation RadioViewController
- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 300)];
    self.headerView.backgroundColor = [UIColor cyanColor];
    
    self.tableView.tableHeaderView = self.headerView;
    
    [self createView];
    [self netWorking];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.textLabel.text = @"1";
    return cell;
}


- (void)netWorking
{
    self.imageArr = [NSMutableArray array];
    NSMutableArray *tempArr = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSDictionary *bodyDic = [NSDictionary dictionaryWithObjectsAndKeys:@"client",@"1",@"deviceid",@"D8FDE94C-841F-4A4D-B5C4-AAD2472CEE85",@"auth",@"",@"version",@"3.0.1", nil];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"application/x-javascript", nil];

    
    [manager POST:@"http://api2.pianke.me/ting/radio" parameters:bodyDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
//        NSDictionary *dic = responseObject;
        
        NSDictionary *dic1 = responseObject[@"data"];
        
        NSArray *arr = dic1[@"carousel"];
        
        for (NSDictionary *tempDic in arr) {
            Scroimage *im = [[Scroimage alloc] init];
            [im setValuesForKeysWithDictionary:tempDic];
            [tempArr addObject:im.img];
        }
        
        [self.imageArr addObject:tempArr[tempArr.count - 1]];
        for (NSInteger i = 0; i < tempArr.count; i++) {
            [self.imageArr addObject:tempArr[i]];
        }
        [self.imageArr addObject:tempArr[0]];
        
        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.imageArr.count, 300);
        
        for (NSInteger i = 0; i < self.scrollView.subviews.count; i++) {
            UIImageView *imageView = self.scrollView.subviews[i];
            [imageView removeFromSuperview];
        }
        
        for (NSInteger i = 0; i < self.imageArr.count; i++) {
            self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, 200)];
            
            [self.scrollView addSubview:self.imageView];
            
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[i]]];
            
            [self.imageView release];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

- (void)createView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    
    self.scrollView.pagingEnabled = YES;
    
    //设置偏移量
    self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    
    [self.headerView addSubview:self.scrollView];
    [_scrollView release];
    
    //加定时器
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
}

- (void)changeImage
{
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + self.view.frame.size.width, 0) animated:YES];
    
    //无限滚
    if (self.scrollView.contentOffset.x == self.view.frame.size.width * (self.imageArr.count - 1)) {
        self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    }
    //所有数据都在block中进行操作
    self.view.backgroundColor = [UIColor cyanColor];
    
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
